package com.edgclub.edg.activity;


import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.edgclub.edg.R;
import com.edgclub.edg.huanxin.DemoHelper;
import com.edgclub.edg.uc.DeleteDialog;
import com.edgclub.edg.uc.SetUpLayout;
import com.edgclub.edg.utils.util.DataCleanUtils;
import com.edgclub.edg.utils.util.StringUtils;
import com.edgclub.edg.utils.util.TopicPrefUtils;
import com.edgclub.edg.utils.util.UserPrefUtils;
import com.hyphenate.EMCallBack;
import com.umeng.analytics.MobclickAgent;

public class SettingActivity extends GzBaseTooolBarActivity implements View.OnClickListener {
    private SetUpLayout person_feedback , change_password , setting_message , about , binding , report;
    private TextView cache_size;
    private ImageView cache_loading;
    private RelativeLayout cache_cleaner;
    private Button close;
    private String getpsw = "2";
    private UserPrefUtils userPrefUtils;
    Animation animation;
    Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case 0:
                    cache_size.setVisibility(View.VISIBLE);
                    cache_loading.clearAnimation();
                    cache_loading.setVisibility(View.GONE);
                    break;

                default:
                    break;
            }
            super.handleMessage(msg);
        }

    };
    @Override
    public int getContentView() {
        return R.layout.activity_setting;
    }

    @Override
    public void setTitleBarView() {
        setCustomActionBar();
        title.setText("设置");
        main_back.setVisibility(View.VISIBLE);
        main_back.setOnClickListener(this);
    }

    @Override
    public void initView() {
        person_feedback = (SetUpLayout) findViewById(R.id.person_feedback);
        change_password = (SetUpLayout) findViewById(R.id.change_password);
        setting_message = (SetUpLayout) findViewById(R.id.setting_message);
        about = (SetUpLayout) findViewById(R.id.about);
        binding = (SetUpLayout) findViewById(R.id.binding);
        cache_size = (TextView) findViewById(R.id.cache_size);
        cache_cleaner = (RelativeLayout) findViewById(R.id.cache_cleaner);
        cache_loading = (ImageView) findViewById(R.id.cache_loading);
        close = (Button) findViewById(R.id.close);
        close.setOnClickListener(this);
        animation = AnimationUtils.loadAnimation(
                this, R.anim.loading_animation);
        person_feedback.setOnClickListener(this);
        change_password.setOnClickListener(this);
        cache_cleaner.setOnClickListener(this);
        setting_message.setOnClickListener(this);
        about.setOnClickListener(this);
        binding.setOnClickListener(this);
        report = (SetUpLayout) findViewById(R.id.report);
        report.setOnClickListener(this);
    }

    @Override
    public void initData() {
        try {
            String size = DataCleanUtils.getTotalCacheSize(this);
            cache_size.setText(size);
        } catch (Exception e) {
            e.printStackTrace();
        }
        userPrefUtils = new UserPrefUtils(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.person_feedback:
                Intent intent1 = new Intent(this,GzFeedBackActivity.class);
                startActivity(intent1);
                break;
            case R.id.change_password:
                Intent intent = new Intent(this,SettingChangePasswordActivity.class);
                startActivity(intent);
                break;
            case R.id.setting_message:
                Intent intent2 = new Intent(this,SettingMessageActivity.class);
                startActivity(intent2);
                MobclickAgent.onEvent(this,"EC_MINE_SETTING_NOTIFICATION");
                break;
            case R.id.about:
                Intent intent3 = new Intent(this,SettingAboutActivity.class);
                startActivity(intent3);
                break;
            case R.id.binding:
                if ("".equals(userPrefUtils.getUser().getPhone())||userPrefUtils.getUser().getPhone().equals(userPrefUtils.getUser().getGzno())) {
                    Intent intent4 = new Intent(this, RegisterActivity.class);
                    intent4.putExtra("getpsw", getpsw);
                    startActivity(intent4);
                    finish();
                }else{
                    Intent intent5 = new Intent(this,SettingChangePhoneActivity.class);
                    startActivity(intent5);
                }
                break;
            case R.id.main_back:
                finish();
                break;
            case R.id.close:
                showLoginOutDialog();
                break;
            case R.id.cache_cleaner:
                cache_loading.setVisibility(View.VISIBLE);
                cache_size.setVisibility(View.GONE);
                cache_loading.startAnimation(animation);
                DataCleanUtils.clearAllCache(this);
                String size = "";
                try {
                    size = DataCleanUtils.getTotalCacheSize(this);
                    cache_size.setText(size);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Message msg = new Message();
                msg.what = 0;
                handler.sendMessageDelayed(msg , 1000);
                break;
            case R.id.report:
//                Intent intent5 = new Intent(this, GzReportListActivity.class);
//                startActivity(intent5);
                break;
        }
    }
    private void showLoginOutDialog() {
        final DeleteDialog dialog = DeleteDialog.create(this, true);
        dialog.setTitle(StringUtils.getString(R.string.leftcontent3));
        dialog.setContent(StringUtils.getString(R.string.leftcontent4));
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
        dialog.setCancelListener(new DeleteDialog.CancelClickListener() {

            @Override
            public void cancelClick() {
                dialog.dismiss();
            }
        });

        dialog.setSubmitListener(new DeleteDialog.SubmitClickListener() {

            @Override
            public void submitClick() {
                dialog.dismiss();
                try {
                    // 先调用sdk logout，在清理app中自己的数据
                    // 清除用户信息
//                    new UserPrefUtils(SettingActivity.this).clearUser();
                    // 退出登录
                    // ActivityUtility.goChooseLoginActivity(activity);

                    logout();
                    com.edgclub.edg.utils.util.ActivityManager.getInstance().finishAllWithoutTop();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    void logout() {
        final ProgressDialog pd = new ProgressDialog(this);
        String st = getResources().getString(R.string.Are_logged_out);
        pd.setMessage(st);
        pd.setCanceledOnTouchOutside(false);
        pd.show();
        DemoHelper.getInstance().logout(false,new EMCallBack() {

            @Override
            public void onSuccess() {
                SettingActivity.this.runOnUiThread(new Runnable() {
                    public void run() {
                        pd.dismiss();
                        // 重新显示登陆页面
                        (SettingActivity.this).finish();
                        Intent intent = new Intent(SettingActivity.this, GzLoginActivity.class);
                        intent.putExtra("remember_phone",userPrefUtils.getUser().getPhone());
                        startActivity(intent);

                    }
                });
                new TopicPrefUtils(SettingActivity.this).setTopicYuehei(false);
            }

            @Override
            public void onProgress(int progress, String status) {

            }

            @Override
            public void onError(int code, String message) {
                SettingActivity.this.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        pd.dismiss();
                        Toast.makeText(SettingActivity.this, "unbind devicetokens failed", Toast.LENGTH_SHORT).show();


                    }
                });
            }
        });
    }

}
